#include "Stdafx.h"
#include "TileSystem.h"

namespace Pulpy
{
	void TileSystem::draw()
	{
		if(_Sprite != NULL)
		{
			glEnable(GL_TEXTURE_2D);
			
			glLoadIdentity();
			//glDisable(GL_DEPTH_TEST);

			glEnable(GL_BLEND);
			//glDepthMask(GL_FALSE);
			glBindTexture(GL_TEXTURE_2D, _Sprite->getTexID());
		}
		//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		for(int i = 0; i < _Rows; ++i)
		{
			for(int j = 0; j < _Cols; ++j)
			{
				if(_TileList[i][j].getType() != 0)
				{
					_Sprite->setIndex(_TileList[i][j].getType());
					if(_Sprite != NULL)
					{	
						_TileList[i][j].draw(_Sprite->getMinX(),_Sprite->getMinY(), _Sprite->getMaxX(), _Sprite->getMaxY());
					}
					else
						_TileList[i][j].draw();
				}
			}
		}
		
		if(_Sprite != NULL)
		{
			glDisable(GL_BLEND);
			glDisable(GL_TEXTURE_2D);
			glEnable(GL_DEPTH_TEST);
		}
	}

	void TileSystem::readTiles(char * filename)
	{
		_Rows = 0;
		_Cols = 0;
		std::ifstream file(filename);
		if(file.is_open())
		{
			int i = 0, k = 0;
			while(file.good())
			{
				std::string aLineOfText = "";
				getline(file, aLineOfText);

				// If this line is not a comment line, then process it
				if(!(aLineOfText.find("//") == NULL) && aLineOfText != "")
				{
					if (_Rows == 0)
					{
						// This is the first line of the map data file
						std::string token;
						std::istringstream iss(aLineOfText);
						while(getline(iss, token, ','))
						{
							// Count the number of columns
							++_Cols;// = atoi(token.c_str());
						}
					}
				}
				_Rows++;
			}
			_TileList = new Tile*[_Rows];
			for(int i = 0; i < _Rows; ++i)
				_TileList[i] = new Tile[_Cols];
			file.clear();
			file.seekg(0, std::ios::beg);
			for(int i = 0; i < _Rows; ++i)
			{
				std::string aLineOfText = "";
				getline(file, aLineOfText);
				int j = 0;
				std::string token;
				std::istringstream iss(aLineOfText);
				while(getline(iss, token, ','))
				{
					_TileList[i][j] = Tile(atoi(token.c_str()),_Width, _Height);
					_TileList[i][j].setArea(i,j);
					if(_TileList[i][j].getType() == 0)
						_TileList[i][j].setSolid(false);
					//setTile(theLineCounter, theColumnCounter, atoi(token.c_str()));
					//_TileList[theLineCounter][theColumnCounter].setType(atoi(token.c_str()));
					_TileList[i][j] .setPosition(j*_Width, i*_Height);
					++j;
				}
			}
		}
		//return true;
	}

	int TileSystem::collideWith(Object2D & ob)
	{
		for(int i = 0; i < _Rows; ++i)
		{
			for(int j = 0; j < _Cols; ++j)
			{
				if(_TileList[i][j].getType() != 0)
				{
					if(_TileList[i][j].Intersects(ob))
					{
						//_TileList[i][j].setType(4);
						return _TileList[i][j].getType();
					}
				}
			}
		}
		return 0;
	}

	int TileSystem::collideTop(Object2D & ob, Tile & t)
	{
		int guessRow = ob.getPosition().getY() / _Height;
		if(guessRow <= 0 || guessRow > _Rows)
			return 0;
		//for(int i = 0; i < guessRow; ++i)
		//{
			for(int j = 0; j < _Cols; ++j)
			{
				if(_TileList[guessRow][j].getType() != 0)
				{
					if(_TileList[guessRow][j].Intersects(ob))
					{
						t =  _TileList[guessRow][j];
						return _TileList[guessRow][j].getType();

					}
				}
			}
		//}
		return 0;
	}

	int TileSystem::collideBelow(Object2D & ob, Tile & t)
	{
		int guessRow = ob.getPosition().getY() / _Height;
		if(guessRow <= 0 || guessRow > _Rows)
			return 0;
		for(int i = guessRow; i < _Rows; ++i)
		{
			for(int j = 0; j < _Cols; ++j)
			{
				if(_TileList[i][j].getType() != 0)
				{
					if(_TileList[i][j].Intersects(ob))
					{
						//_TileList[i][j].setType(3);
						t =  _TileList[i][j];
						return _TileList[i][j].getType();

					}
				}
			}
		}
		return 0;
	}

	int TileSystem::collideRight(Object2D & ob, Tile & t)
	{
		int guessRow = ob.getPosition().getY() / _Height;
		int guessCol = ob.getPosition().getX() / _Width;
		if(guessRow <= 0 || guessRow > _Rows)
			return 0;
		if(guessCol <= 0 || guessCol > _Cols)
			return 0;
		//for(int i = guessRow-1; i < _Rows; ++i)
		//{
			for(int j = guessCol; j < _Cols; ++j)
			{
				if(_TileList[guessRow][j].getType() != 0)
				{
					if(_TileList[guessRow][j].Intersects(ob))
					{
						//_TileList[guessRow][j].setType(3);
						t = _TileList[guessRow][j];
						return _TileList[guessRow][j].getType();
					}
				}
			}
		//}
		return 0;
	}

	int TileSystem::collideLeft(Object2D & ob, Tile & t)
	{
		int guessRow = ob.getPosition().getY() / _Height;
		int guessCol = ob.getPosition().getX() / _Width;
		if(guessRow <= 0 || guessRow > _Rows)
			return 0;
		if(guessCol <= 0 || guessCol > _Cols)
			return 0;
		//for(int i = guessRow-1; i < _Rows; ++i)
		//{
			for(int j = 0; j < guessCol+1; ++j)
			{
				if(_TileList[guessRow][j].getType() != 0)
				{
					if(_TileList[guessRow][j].Intersects(ob))
					{
						//_TileList[guessRow][j].setType(3);
						t = _TileList[guessRow][j];
						return _TileList[guessRow][j].getType();
					}
				}
			}
		//}
		return 0;
	}

	void TileSystem::saveTiles(char * filename)
	{

	}


}